﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Warehouse.BusinessLogic;
using Warehouse.Common.Entities;

namespace Warehouse.UI.WinForm.Forms
{
    public partial class UserBrowseForm : Warehouse.UI.WinForm.Forms.BrowseForm
    {
        //--Constructor
        public UserBrowseForm()
        {
            InitializeComponent();

            this.DataGridViewColumns = _getColumns();
        }

        protected override void InitLoad()
        {
            //this.TitleText = "用户列表";
            //ltbCode.LabelText = "编号";
            //ltbFullName.LabelText = "名称";

            this.ListState = BrowseState.CanAddItem & BrowseState.CanEditItem;

            //ltbCode.TextBoxTextChanged += this.OnConditionChanged;
            //ltbFullName.TextBoxTextChanged += this.OnConditionChanged;
        }

        protected override void ClearFilter()
        {
            //ltbCode.TextBoxText = "";
            //ltbFullName.TextBoxText = "";
        }

        protected override IEnumerable GetFilteredItems()
        {
            var bl = UserBL.Instance;
            var users = bl.GetList(null);

            //var sCode = ltbCode.TextBoxText.Trim().ToUpper();
            //var sName = ltbFullName.TextBoxText.Trim().ToUpper();

            return (from x in users
                    //where (sCode == string.Empty || x.Code.ToUpper().Contains(sCode))
                    //&& (sName == string.Empty || x.FullName.ToUpper().Contains(sName))
                    //orderby x.FullName
                    select x).ToList();
        }

        protected override bool AddItem()
        {
            var f = new UserEditForm();
            f.Add();
            return (f.ShowDialog() == DialogResult.OK);
        }

        private int CurrentEntryID
        {
            get
            {
                return (dataGridViewContent.CurrentRow == null)
                           ? 0
                           : (dataGridViewContent.CurrentRow.DataBoundItem as User).UserID;
            }
        }

        protected override bool EditItem()
        {
            var f = new UserEditForm();
            f.Edit(this.CurrentEntryID);
            return (f.ShowDialog() == DialogResult.OK);
        }

        protected override bool DeleItem()
        {
            var f = new UserEditForm();
            f.Delete(this.CurrentEntryID);
            return (f.ShowDialog() == DialogResult.OK);
        }

        protected override bool ViewItem()
        {
            var f = new UserEditForm();
            f.View(this.CurrentEntryID);
            return (f.ShowDialog() == DialogResult.OK);
        }

        #region GetColumns

        private static DataGridViewColumn[] _getColumns()
        {
            var colUserID = new DataGridViewTextBoxColumn()
            {
                Name = "colUserID",
                DataPropertyName = "UserID",
                HeaderText = "colUserID",
                ReadOnly = true,
                Visible = false,
            };
            var colUserCode = new DataGridViewTextBoxColumn()
            {
                Name = "colUserCode",
                DataPropertyName = "UserCode",
                HeaderText = "编号",
                Width = 100,
            };
            var colUserName = new DataGridViewTextBoxColumn()
            {
                Name = "colUserName",
                DataPropertyName = "UserName",
                HeaderText = "名称",
                Width = 200,
            };
            var colUserGroupID = new DataGridViewTextBoxColumn()
            {
                Name = "colUserGroupID",
                DataPropertyName = "UserGroupID",
                HeaderText = "所属分类",
                Width = 100,
            };
            var colIsEnable = new DataGridViewCheckBoxColumn()
            {
                Name = "colIsEnable",
                DataPropertyName = "IsEnable",
                HeaderText = "停用",
                Width = 40,
            };


            return new DataGridViewColumn[]
            {
               colUserID,
               colUserCode,
               colUserName,
               colUserGroupID,
               colIsEnable,
            };
        }

        #endregion
    }
}
